﻿using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using spbtzb;
using System.Data.OleDb;

public partial class _Default : System.Web.UI.Page 
{
    //protected string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='./spbtzb/App_Data/spbtzb.mdb';Persist Security Info=True;Jet OLEDB:Database Password=cqusoftware";// + System.Configuration.ConfigurationManager.AppSettings["ConnString"] + "'";
    //protected string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='E://spbtzb.mdb';";
    protected string strConnection = spbtzb.ConString.ConnString;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindNav();
            Ddl_Types_BindData();
            BingSTImage();
        }
    }

    protected void BindNav()
    {
        rpNav.DataSource = AccessHelper.ExecuteDataTable(ConString.ConnString, CommandType.Text,
                             "SELECT ID, TypeCode, TypeName FROM ContentType WHERE ParentType = '" + Common.PARENT_CODE + "'");
        rpNav.DataBind();

    }

    private void Ddl_Types_BindData()
    {
        // generate datatable                       
        string sqlCmd = "SELECT * FROM ContentType WHERE ParentType = 'NO'";
        System.Data.DataTable dtTypes = AccessHelper.ExecuteDataTable(strConnection, System.Data.CommandType.Text, sqlCmd);
        if (dtTypes == null || dtTypes.Rows.Count == 0)
        {
            // 没有相应类型的信息
            // RegisterClientScriptBlock("tip", "<script>alert('没有相应的类型数据！');</script>");
            return;
        }
        this.ddl_Types.DataSource = dtTypes;
        this.ddl_Types.DataBind();
        this.ddl_Types.Items.Add(new ListItem("全部", "all"));
    }

    protected void btn_Search_Click(object sender, EventArgs e)
    {
        if (!String.IsNullOrEmpty(TB_Search.Text.Trim()))
        {
            Response.Redirect("SearchResult.aspx?Type=" + ddl_Types.SelectedValue + "&KeyWords=" + TB_Search.Text);
        }
    }

    private void BingSTImage()
    {
        OleDbConnection conn = new OleDbConnection(ConString.ConnString);
        conn.Open();

        //绑定顶部专题图片
        DataTable dtSTA = AccessHelper.ExecuteDataTable(conn, CommandType.Text, "SELECT TOP 1 ImageURL,Title, LinkURL FROM ImageList WHERE Type='STA' AND IsDisplay=true ORDER BY [ID] DESC");
        if (dtSTA.Rows.Count == 1)
        {
            hlSTA.NavigateUrl = dtSTA.Rows[0]["LinkURL"].ToString();
            imageSTA.ImageUrl = "imagesUpload/"+dtSTA.Rows[0]["ImageURL"].ToString();
        }
        else
        {
            hlSTA.Visible = false;
            imageSTA.Visible = false;
        }

        //绑定中部专题图片
        DataTable dtSTB = AccessHelper.ExecuteDataTable(conn, CommandType.Text, "SELECT TOP 1 ImageURL,Title, LinkURL FROM ImageList WHERE Type='STB' AND IsDisplay=true ORDER BY [ID] DESC");
        if (dtSTB.Rows.Count == 1)
        {
            hlSTB.NavigateUrl = dtSTB.Rows[0]["LinkURL"].ToString();
            imageSTB.ImageUrl = "imagesUpload/" + dtSTB.Rows[0]["ImageURL"].ToString();
        }
        else
        {
            hlSTB.Visible = false;
            imageSTB.Visible = false;
        }

        //绑定顶部专题图片
        DataTable dtSTC = AccessHelper.ExecuteDataTable(conn, CommandType.Text, "SELECT TOP 3 ImageURL,Title, LinkURL FROM ImageList WHERE Type='STC' AND IsDisplay=true ORDER BY [ID] DESC");
        RepeaterSTC.DataSource = dtSTC;
        RepeaterSTC.DataBind();

        conn.Close();
    }
}
